<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="addGoodsCate">添加分类</el-button>
        </el-col>
      </el-row>
      <tree-table
        :data="goodsCateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        :show-row-hover="false"
        border
        :show-index="true"
        index-text="#"
      >
      <template slot="isuse" slot-scope="scope">
        <i class="el-icon-success" :class="['success']" v-if="scope.row.cat_deleted = 'false'"></i>
        <i class="el-icon-error" :class="['error']" v-else></i>
      </template>
      <template slot="sort" slot-scope="scope">
        <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
        <el-tag type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
        <el-tag type="warning" v-else>三级</el-tag>
      </template>
      <template slot="option" slot-scope="scope">
        <el-button type="primary" icon="el-icon-edit" size="mini" @click="getEditCate(scope.row.cat_id)">编辑</el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteCate(scope.row.cat_id)">删除</el-button>
      </template>
      </tree-table>
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[1, 3, 5, 9]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
    <el-dialog
  title="添加分类"
  :visible.sync="addGoodsCateDialogVisible"
  width="50%"
  @close="closeAddGoodsCateDialog"
  >
  <el-form :model="addGoodsCateList" ref="addGoodsCateListRef" :rules="addGoodsCateListRule">
    <el-form-item label="分类名称" label-width="100px" prop="cat_name">
      <el-input v-model="addGoodsCateList.cat_name"></el-input>
    </el-form-item>
    <el-form-item label="父级分类" label-width="100px">
      <el-cascader
    v-model="checkedArray"
    :options="jlGoodsCateList"
    :props="cascaderProps"
    @change="showCheckedArray"
    clearable
    ></el-cascader>
    </el-form-item>
  </el-form>
  <span slot="footer">
    <el-button @click="addGoodsCateDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitAddGoodsCate">确 定</el-button>
  </span>
</el-dialog>
<el-dialog
  title="编辑分类"
  :visible.sync="editGoodsCateDialogVisible"
  width="50%"
  @close="closeEditGoodsCateDialog"
  >
  <el-form :model="editCateList" ref="editCateListRef" :rules="addGoodsCateListRule">
    <el-form-item label="分类名称" label-width="100px" prop="cat_name">
      <el-input v-model="editCateList.cat_name"></el-input>
    </el-form-item>
  </el-form>
  <span slot="footer">
    <el-button @click="editGoodsCateDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitEditGoodsCate">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>
<script>
export default {
  name: 'cate',
  data () {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      goodsCateList: [],
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          type: 'template',
          template: 'isuse'
        },
        {
          label: '排序',
          prop: 'cat_level',
          type: 'template',
          template: 'sort'
        },
        {
          label: '排序',
          type: 'template',
          template: 'option'
        }
      ],
      addGoodsCateDialogVisible: false,
      addGoodsCateList: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      addGoodsCateListRule: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      jlGoodsCateList: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover'
      },
      checkedArray: [],
      editCateList: [],
      editGoodsCateDialogVisible: false
    }
  },
  methods: {
    async getGoodsCateList () {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return
      this.goodsCateList = res.data.result
      this.total = res.data.total
      console.log(res.data)
    },
    handleSizeChange (newsize) {
      this.queryInfo.pagesize = newsize
      this.getGoodsCateList()
    },
    handleCurrentChange (newpage) {
      this.queryInfo.pagenum = newpage
      this.getGoodsCateList()
    },
    async addGoodsCate () {
      this.addGoodsCateDialogVisible = true
      const { data: res } = await this.$http.get('categories', {
        params: {
          type: 2
        }
      })
      if (res.meta.status !== 200) return
      this.jlGoodsCateList = res.data
      console.log(this.jlGoodsCateList)
    },
    showCheckedArray () {
      if (this.checkedArray.length > 0) {
        this.addGoodsCateList.cat_pid = this.checkedArray[this.checkedArray.length - 1]
        this.addGoodsCateList.cat_level = this.checkedArray.length
      } else {
        this.addGoodsCateList.cat_pid = 0
        this.addGoodsCateList.cat_level = 0
      }
    },
    closeAddGoodsCateDialog () {
      console.log('我执行了11111')
      this.$refs.addGoodsCateListRef.resetFields()
      this.checkedArray = []
      this.addGoodsCateList.cat_pid = 0
      this.addGoodsCateList.cat_level = 0
    },
    submitAddGoodsCate () {
      console.log('我执行了')
      console.log(this.addGoodsCateList)
      this.$refs.addGoodsCateListRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.addGoodsCateList)
        console.log(res)
        if (res.meta.status !== 201) return this.$message.error('新增分类失败')
        this.$message.success('新增分类成功')
        this.getGoodsCateList()
      })
      // this.addGoodsCateDialogVisible = false
    },
    // 编辑分类start
    async getEditCate (id) {
      const { data: res } = await this.$http.get(`categories/${id}`)
      if (res.meta.status !== 200) return
      this.editCateList = res.data
      this.editGoodsCateDialogVisible = true
    },
    closeEditGoodsCateDialog () {
      this.$refs.editCateListRef.resetFields()
    },
    submitEditGoodsCate () {
      this.$refs.editCateListRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put(`categories/${this.editCateList.cat_id}`, {
          cat_name: this.editCateList.cat_name
        })
        if (res.meta.status !== 200) return this.$message.error('更新分类失败')
        this.$message.success('更新分类成功')
        this.getGoodsCateList()
      })
      this.editGoodsCateDialogVisible = false
    },
    async deleteCate (id) {
      const userDeleteCateResponse = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (userDeleteCateResponse !== 'confirm') this.$message.info('您取消了删除')
      const { data: res } = await this.$http.delete('categories/' + id)
      if (res.meta.status !== 200) return this.$message.error('删除分类失败')
      this.$message.success(res.meta.msg)
      this.getGoodsCateList()
    }
  },
  created () {
    this.getGoodsCateList()
  }
}
</script>
<style lang="less" scoped>
  .success {
    color:lightgreen
  }
  .error {
    color: red
  }
  .zk-table {
    margin: 15px 0;
  }
  .el-cascader{
    width: 100%;
  }
</style>
